/*iiqt8aVY/augU9fW1NuaVCGrewSqSoCegRS/j4FGQcaOYvAza3RNZPH/zeXX/3aF*/
/*
 * @Author: xiahui
 * @PageInfo: 卡片工具
 * @Date: 2019-01-17 11:16:04
 * @Last Modified by: huoyzh
 * @Last Modified time: 2020-05-18 10:18:12
 */
import {
	AREA,
	DATASOURCECACHE,
	FIELDS,
	UISTATUS,
	FSTATUSFLAG,
	BUTTONID,
	CARDTABLEAREAIDS,
	CACHESTATUS
} from '../constance';
import { addCacheData, updateCacheData, deleteCacheData, updatePKCache } from '../../../../scmpub/scmpub/pub/cache';
import { buttonControl } from '../card/viewControl/buttonControl';
import { updateExtBillDataForCompareByPk } from '../../../../scmpub/scmpub/pub/tool/cardTableTools/compareUtils';
import { headAfterEvent } from '../card/afterEvents';

/**
 * 清空卡片数据
 * @param {*} props
 */
function clearCardData(props) {
	props.form.EmptyAllFormValue(AREA.cardFormId);
	CARDTABLEAREAIDS.forEach((areaId) => {
		props.cardTable.setTableData(areaId, { rows: [] });
	});
}

/**
 * 设置卡片界面：数据、按钮状态
 * @param {*} props
 * @param {*} data
 * @param {*} isCompare 差异比较
 */
function setCardPage(props, data, isCompare = true, pageStatus) {
	props.beforeUpdatePage();
	if (data.head && data.head.head) {
		props.form.setAllFormValue({ [AREA.cardFormId]: data.head[AREA.cardFormId] });
	}
	if (data.bodys) {
		CARDTABLEAREAIDS.forEach((areaId) => {
			if (data.bodys[areaId]) {
				isCompare
					? (data.bodys[areaId] = props.cardTable.updateDiffDataByRowId(
							areaId,
							data.bodys[areaId],
							true,
							false, //add by huoyzh 差异比较 此时由true改为false
							false
						))
					: props.cardTable.setTableData(areaId, data.bodys[areaId], null, true, true);
				props.cardTable.selectAllRows(areaId, false); // 取消表体勾选
			} else {
				props.cardTable.setTableData(areaId, { rows: [] });
			}
		});
	}

	// 更新按钮状态
	buttonControl.call(this, props, pageStatus);
	props.updatePage(AREA.cardFormId, CARDTABLEAREAIDS);
}

/**
 * 设置卡片缓存数据
 * @param {*} props
 * @param {*} data
 * @param {*} cacheStatus
 */
function setCardCacheData(props, data, status = CACHESTATUS.update, modifyPK) {
	let pk = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.pk_ct_pu).value;
	if (status == CACHESTATUS.add || status == CACHESTATUS.ref20 || status == CACHESTATUS.ref28) {
		// 新增缓存数据
		addCacheData(props, FIELDS.pk_ct_pu, pk, data, AREA.cardFormId, DATASOURCECACHE.dataSourceListCacheKey);
		// 更新翻页组件当前pk值
		props.cardPagination.setCardPaginationId({ id: pk, status: 1 });
	} else if (status == CACHESTATUS.update) {
		// 更新缓存数据
		updateCacheData(props, FIELDS.pk_ct_pu, pk, data, AREA.cardFormId, DATASOURCECACHE.dataSourceListCacheKey);
	} else if (status == CACHESTATUS.delete) {
		// 删除缓存数据
		deleteCacheData(props, FIELDS.pk_ct_pu, data.successIds[0], DATASOURCECACHE.dataSourceListCacheKey);
	}
	if (status == CACHESTATUS.modify || modifyPK) {
		//deleteCacheData(props, FIELDS.pk_ct_pu, modifyPK, DATASOURCECACHE.dataSourceListCacheKey);
		updatePKCache4card(props, modifyPK, data, AREA.cardFormId, DATASOURCECACHE.dataSourceListCacheKey);
	}
}

/**
 * 差异更新卡片界面
 * @param {*} props
 * @param {*} data
 * @param {*} config
 */
function setCardCompareData(props, data, config) {
	props.beforeUpdatePage();
	let oldPKValue = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.pk_ct_pu).value;
	// 差异更新界面
	updateExtBillDataForCompareByPk(props, data, config);
	// let pk = data.head.head.rows[0].values.pk_ct_pu.value;
	data.head[AREA.cardFormId].rows = props.form.getAllFormValue(AREA.cardFormId).rows;
	// deleteCacheData(props, FIELDS.pk_ct_pu, oldPKValue, DATASOURCECACHE.dataSourceListCacheKey);
	// addCacheData(props, FIELDS.pk_ct_pu, pk, data, AREA.cardFormId, DATASOURCECACHE.dataSourceListCacheKey);
	// 更新卡片按钮状态
	buttonControl.call(this, props);
	// 更新缓存
	updatePKCache4card(props, oldPKValue, data, AREA.cardFormId, DATASOURCECACHE.dataSourceListCacheKey);
	props.updatePage(AREA.cardFormId, CARDTABLEAREAIDS);
}

/**
 * 合同缓存替换或是更新，合同变更时主键发生改变，此时要替换缓存
 * @param {*} props
 * @param {*} oldPKValue
 * @param {*} newData
 * @param {*} headAreaCode
 * @param {*} dataSource
 */
function updatePKCache4card(props, oldPKValue, newData, headAreaCode, dataSource) {
	let newPKValue = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.pk_ct_pu).value;
	if (newPKValue != oldPKValue) {
		updatePKCache(dataSource, newPKValue, oldPKValue, headAreaCode, newData);
	} else {
		updateCacheData(props, FIELDS.pk_ct_pu, newPKValue, newData, headAreaCode, dataSource);
	}
}

/**
 * 生效态单据进行变更时，需要在变更历史表格新增一行
 * @param {*} props
 */
function addRowForModify(props) {
	let status = props.getUrlParam('status'); // 页面状态
	let fstatusflag = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.fstatusflag).value; // 合同状态
	if (status != UISTATUS.modify || fstatusflag != FSTATUSFLAG.validate) return;
	let newVersion = parseInt(props.form.getFormItemsValue(AREA.cardFormId, FIELDS.version).value) + 1; // 版本号
	// 更新表头版本号
	props.form.setFormItemsValue(AREA.cardFormId, { [FIELDS.version]: { value: newVersion } });
	// 变更历史表格添加一行
	let rowCount = props.cardTable.getNumberOfRows(AREA.cardChangeId);
	props.cardTable.addRow(AREA.cardChangeId, rowCount, { [FIELDS.vchangecode]: { value: newVersion } }, true);
	props.cardTable.toggleCardTable(AREA.cardChangeId, true);
}

/**
 * 当主组织为空时，设置页面
 * @param {*} props
 */
function setCardPageForEmptyPk_org_v(props) {
	props.form.setFormItemsDisabled(AREA.cardFormId, { pk_org_v: false });
	props.initMetaByPkorg(FIELDS.pk_org_v);
	props.button.setDisabled({
		[BUTTONID.AddLine]: true,
		[BUTTONID.DeleteLine]: true,
		[BUTTONID.CopyLine]: true,
		[BUTTONID.InsertLine]: true,
		[BUTTONID.ReRangeRowNo]: true
	});
}

/**
 * 设置卡片表体默认收起
 * @param {*} props
 */
function toggleCardTable(props) {
	props.cardTable.toggleCardTable(AREA.cardTableId, true);
	props.cardTable.toggleCardTable(
		[ AREA.cardTermId, AREA.cardPayId, AREA.cardFeeId, AREA.cardMemoraId, AREA.cardChangeId, AREA.cardExecutId ],
		false
	);
}

/**
 * 根据模板获取对应的区域ID和按钮ID
 * @param {*} moduleId 模板ID
 * @param {*} moduleId 页面状态
 * @param {*} copyData 复制行数据
 */
function getAreaAndBtnId(moduleId, status, copyData) {
	let bodyAreaId, InnerAreaId, InnerBtnIds;

	switch (moduleId) {
		case AREA.cardTableId:
			bodyAreaId = AREA.card_body;
			InnerAreaId = AREA.card_body_inner;
			InnerBtnIds =
				status == UISTATUS.browse
					? [ BUTTONID.Open ]
					: copyData
						? [ BUTTONID.PasteLine ]
						: [ BUTTONID.Open, BUTTONID.DeleteLine, BUTTONID.CopyLine, BUTTONID.InsertLine ];
			break;
		case AREA.cardTermId:
			bodyAreaId = AREA.card_term;
			InnerAreaId = AREA.card_term_inner;
			InnerBtnIds =
				status == UISTATUS.browse ? [] : copyData ? [ BUTTONID.PasteLineTerm ] : [ BUTTONID.DeleteLineTerm ];
			break;
		case AREA.cardPayId:
			bodyAreaId = AREA.card_pay;
			InnerAreaId = AREA.card_pay_inner;
			InnerBtnIds =
				status == UISTATUS.browse ? [] : copyData ? [ BUTTONID.PasteLinePay ] : [ BUTTONID.DeleteLinePay ];
			break;
		case AREA.cardFeeId:
			bodyAreaId = AREA.card_fee;
			InnerAreaId = AREA.card_fee_inner;
			InnerBtnIds =
				status == UISTATUS.browse ? [] : copyData ? [ BUTTONID.PasteLineFee ] : [ BUTTONID.DeleteLineFee ];
			break;
		case AREA.cardMemoraId:
			bodyAreaId = AREA.card_memora;
			InnerAreaId = AREA.card_memora_inner;
			InnerBtnIds =
				status == UISTATUS.browse
					? []
					: copyData ? [ BUTTONID.PasteLineMemora ] : [ BUTTONID.DeleteLineMemora ];
			break;
	}

	return { bodyAreaId: bodyAreaId, InnerAreaId: InnerAreaId, InnerBtnIds: InnerBtnIds };
}

/**
 * 默认主组织
 * @param {*} props
 * @param {*} context
 */
function mainOrgInit(props, context) {
	let pk_org_v = context.pk_org_v;
	if (pk_org_v) {
		props.form.setFormItemsDisabled(AREA.cardFormId, { pk_org_v: false });
		headAfterEvent.call(this, props, AREA.cardFormId, FIELDS.pk_org_v, { value: pk_org_v });
	} else {
		setCardPageForEmptyPk_org_v.call(this, props);
	}
}

/**
 * 付款协议插行、增行、删除行、粘贴至末行、粘贴至此时，重置付款期值
 * @param {*} props
 * @param {*} moduleId
 */
function resetShoworderValue(props, moduleId) {
	if (moduleId == AREA.cardPayId) {
		let totalNum = props.cardTable.getNumberOfRows(moduleId);
		for (let i = 0; i < totalNum; i++) {
			props.cardTable.setValByKeyAndIndex(moduleId, i, FIELDS.showorder, { value: i + 1 + '' });
		}
	}
}

export {
	clearCardData,
	setCardPage,
	setCardCompareData,
	addRowForModify,
	setCardPageForEmptyPk_org_v,
	toggleCardTable,
	setCardCacheData,
	getAreaAndBtnId,
	mainOrgInit,
	resetShoworderValue
};

/*iiqt8aVY/augU9fW1NuaVCGrewSqSoCegRS/j4FGQcaOYvAza3RNZPH/zeXX/3aF*/